package work.jame.blog.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import work.jame.blog.framework.VerificationGroup;
import work.jame.blog.framework.exception.BusinessException;
import work.jame.blog.framework.exception.db.UpdateDataBaseException;
import work.jame.blog.framework.limit.RequestLimit;
import work.jame.blog.framework.result.Result;
import work.jame.blog.pojo.User;
import work.jame.blog.pojo.dto.UserDTO;
import work.jame.blog.service.UserService;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Jame
 * @since 2023-05-09
 */

@Api(tags = "用户")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestLimit
    @ApiOperation("登录")
    @PostMapping("/login")
    public Result<?> login(@Validated(VerificationGroup.Login.class) @RequestBody UserDTO userDTO) {
        return Result.succeed(userService.login(userDTO));
    }

    @ApiOperation("登出")
    @GetMapping("/logout")
    public Result<?> logout() {
        userService.logout();
        return Result.succeed();
    }

    @RequestLimit(value = 20, timeUnit = TimeUnit.MINUTES)
    @ApiOperation("创建登录验证码")
    @ApiImplicitParam(value = "用户邮箱", name = "email")
    @GetMapping("/createLoginCode")
    public void createLoginCode(@RequestParam("email") String email) {
        userService.createLoginCode(email);
    }

    @RequestLimit(value = 20, timeUnit = TimeUnit.MINUTES)
    @ApiOperation("创建登录邮箱验证码")
    @ApiImplicitParam(value = "用户邮箱", name = "email")
    @GetMapping("/createLoginEmailCode")
    public Result<?> createLoginEmailCode(@RequestParam("email") String email) {
        userService.createLoginEmailCode(email);
        return Result.succeedMessage("验证码已发送到您的邮箱,请注意查看");
    }


    @RequestLimit(value = 20, timeUnit = TimeUnit.MINUTES)
    @ApiOperation("创建注册验证码")
    @ApiImplicitParam(value = "用户邮箱", name = "email")
    @GetMapping("/createRegisterCode")
    public void createRegisterCode(@RequestParam("email") String email) {
        userService.createRegisterCode(email);
    }


    @ApiOperation("创建注册邮箱验证码")
    @ApiImplicitParam(value = "用户邮箱", name = "email")
    @GetMapping("/createRegisterEmailCode")
    public Result<?> createRegisterEmailCode(@RequestParam("email") String email, @RequestParam("code") String code) {
        userService.createRegisterEmailCode(email, code);
        return Result.succeedMessage("验证码已发送到您的邮箱,请注意查看");
    }


    @RequestLimit(value = 20, timeUnit = TimeUnit.MINUTES)
    @ApiOperation("创建找回密码验证码")
    @ApiImplicitParam(value = "用户邮箱", name = "email")
    @GetMapping("/createFindPasswordCode")
    public void createFindPasswordCode(@RequestParam("email") String email) {
        userService.createFindPasswordCode(email);
    }


    @ApiOperation("创建找回密码邮箱验证码")
    @ApiImplicitParam(value = "用户邮箱", name = "email")
    @GetMapping("/createFindPasswordEmailCode")
    public Result<?> createFindPasswordEmailCode(@RequestParam("email") String email, @RequestParam("code") String code) {
        userService.createFindPasswordEmailCode(email, code);
        return Result.succeedMessage("验证码已发送到您的邮箱,请注意查看");
    }


    @RequestLimit(timeUnit = TimeUnit.MINUTES)
    @ApiOperation("注册")
    @PostMapping("/register")
    public Result<?> register(@Validated(VerificationGroup.Save.class) @RequestBody UserDTO userDTO) {
        if (!StringUtils.hasText(userDTO.getEmailCode())) {
            throw new IllegalArgumentException("邮箱验证码不能为空");
        }
        userService.register(userDTO);
        return Result.succeedMessage("注册成功!");
    }

    @RequestLimit
    @ApiOperation("查询邮箱是否已经注册")
    @GetMapping("/queryEmailExist")
    public Result<?> queryEmailExist(@RequestParam("email") String email) {
        boolean exist = userService.queryEmailExist(email);
        if (exist) {
            return Result.succeed(true, "该邮箱已注册");
        }
        return Result.succeed(false, "该邮箱未注册");
    }


    @RequestLimit(2)
    @ApiOperation("查询用户信息基础数据")
    @GetMapping("/queryUserInfo")
    public Result<?> queryUserInfo() {
        return Result.succeed(userService.queryUserInfo());
    }


    @RequestLimit
    @ApiOperation("重新设置用户密码")
    @PostMapping("/resetPassword")
    public Result<?> resetPassword(@Validated(VerificationGroup.Update.class) @RequestBody UserDTO userDTO) {
        userService.resetPassword(userDTO);
        return Result.succeed();
    }


    @RequestLimit
    @ApiOperation("更新用户信息")
    @PostMapping("/updateInfo")
    public Result<?> updateInfo(@Validated(VerificationGroup.Update.class) @RequestBody User user) {
        if (StringUtils.hasText(user.getPassword())) {
            throw new BusinessException("非法传入参数");
        }
        if (!userService.updateUserData(user)) {
            throw new UpdateDataBaseException("修改用户信息失败");
        }
        return Result.succeed();
    }

    @RequestLimit(value = 3, timeUnit = TimeUnit.MINUTES)
    @GetMapping("/hello")
    public Result<?> hello() {
        return Result.succeed("hello!");
    }


    @RequestLimit(value = 5)
    @GetMapping("/fuzzyQueryByUserNickName")
    public Result<?> fuzzyQueryByUserNickName(@RequestParam("nickName") String nickName) {
        List<User> users = userService.fuzzyQueryByUserNickNameLimit8(nickName);
        if (Objects.isNull(users) || users.isEmpty()) {
            return Result.noData();
        }
        return Result.succeed(users);
    }

}
